package com.ffcs.web.controllers;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ffcs.web.model.dto.AdvertPicResponse;
import com.ffcs.web.model.dto.QuestionType;
import com.ffcs.web.model.dto.QuestionTypeResponse;
import com.ffcs.web.model.entity.Advert;
import com.ffcs.web.model.entity.WechatCanyueyue;
import com.ffcs.web.services.AdvertService;
import com.ffcs.web.utils.CommonConfig;

@Controller
public class AdvertController {

	private Logger logger = Logger.getLogger(AdvertController.class);
	
	@Autowired
	private AdvertService advertService;
	
	/**
	 * 广告列表
	 * @param type
	 * @return
	 */
	@RequestMapping("getAdvertPic")
	@ResponseBody
	public AdvertPicResponse getAdvertPic(String type){
		AdvertPicResponse advertPicResponse=new AdvertPicResponse();
		advertPicResponse.setResultCode("0");
		advertPicResponse.setResultDesc("success");
		List<Advert> advertPicList=advertService.getAdvertPic(type); 
		advertPicResponse.setAdvertPicList(advertPicList);
		return advertPicResponse;
	}
	
	/**
	 * 进入公众菜单的最新活动页
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("mostNewActivity")
	public void mostNewActivity(HttpServletRequest request,HttpServletResponse response) throws IOException{
		List<Advert> advertPicList=advertService.getAdvertPic("1");
		response.sendRedirect("gotoPicNews?advertId="+advertPicList.get(0).getId());
	}
	
	/**
	 * 广告列表
	 * @param type
	 * @return
	 */
	@RequestMapping("getAdvertPic2")
	@ResponseBody
	public AdvertPicResponse getAdvertPic2(String advertType){
		AdvertPicResponse advertPicResponse=new AdvertPicResponse();
		advertPicResponse.setResultCode("0");
		advertPicResponse.setResultDesc("success");
		List<Advert> advertPicList=advertService.getAdvertPic2(advertType); 
		for (Advert ad: advertPicList) {
			ad.setUrl(CommonConfig.getProperty("PIC_URL_FRONT")+ad.getUrl());
		}
		advertPicResponse.setAdvertPicList(advertPicList);
		return advertPicResponse;
	}
	
	/**
	 * 问题分类
	 * @param type
	 * @return
	 */
	@RequestMapping("getQuestionType")
	@ResponseBody
	public QuestionTypeResponse getQuestionType(String type){
		QuestionTypeResponse questionTypeResponse=new QuestionTypeResponse();
		questionTypeResponse.setResultCode("0");
		questionTypeResponse.setResultDesc("success");
		List<QuestionType> questionTypeList=advertService.getQuestionType(type); 
		for (QuestionType ad: questionTypeList) {
			ad.setPic1(CommonConfig.getProperty("PIC_URL_FRONT")+ad.getPic1());
			ad.setPic2(CommonConfig.getProperty("PIC_URL_FRONT")+ad.getPic2());
			ad.setPic3(CommonConfig.getProperty("PIC_URL_FRONT")+ad.getPic3());
		}
		questionTypeResponse.setQuestionTypeList(questionTypeList);
		return questionTypeResponse;
	}
}
